/**********************************************************************
*  Copyright (c) 2007 - Victor Jacobs - victor.jacobs@gmail.com
*
*  Permission is hereby granted, free of charge, to any person
*  obtaining a copy of this software and associated documentation
*  files (the "Software"), to deal in the Software without
*  restriction, including without limitation the rights to use,
*  copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the
*  Software is furnished to do so, subject to the following
*  conditions:
*
*  The above copyright notice and this permission notice shall be
*  included in all copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*  OTHER DEALINGS IN THE SOFTWARE.
**********************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Vj.GA.Functions;

namespace Vj.GATool.Controls
{
    public partial class Plot2DPanel : UserControl
    {
        private Graphics myGraphic;
        private int plotResolution;
        private AFunction function2D;

        private bool doDrawFunction;
        private bool doDrawAxis;

        public Plot2DPanel()
        {
            InitializeComponent();
            doDrawAxis = true;
            doDrawFunction = true;
            myGraphic = this.CreateGraphics();
            function2D = TwoDFunctions.XCubed;
            plotResolution = 100;
        }

        public bool DoDrawFunction
        {
            get { return doDrawFunction; }
            set 
            { 
                doDrawFunction = value;
                this.Refresh();
            }
        }
        public bool DoDrawAxis
        {
            get { return doDrawAxis;}
            set 
            { 
                doDrawAxis = value;
                this.Refresh(); 
            }
        }

        public void DrawAxis(Pen pen)
        {
            float w = (float) this.Width;
            float h = (float) this.Height;

            // draw axis
            this.DrawLine(pen, -w/2.0f, 0, w/2.0f, 0);
            this.DrawLine(pen, 0, h/2.0f, 0, -h/2.0f);
        }
        public void DrawFunction(Pen pen, AFunction aFunction)
        {
            PointF[] points;
            points = new PointF[plotResolution];

            int w = this.Width;
            int h = this.Height;
            int r = plotResolution;

            for (int i = 0; i < r; i++)
            {
                // lerp
                double t = (1 - ((double)i / r)) * (-w / 2) + ((double)i / r) * (w / 2);
                double[] val = new double[1];
                val[0] = t;
                points[i] = new Point((int)t, (int)aFunction(val));
            }

            this.DrawLines(pen, points);
        }

        private void DrawLines(Pen pen, PointF[] points)
        {
            for (int i = 0; i < points.Length - 1; i++)
                this.DrawLine(pen, points[i].X, points[i].Y, points[i + 1].X, points[i + 1].Y);
        }
    
        private void Plot2DPanel_Paint(object sender, PaintEventArgs e)
        {
            if (doDrawAxis)
                DrawAxis(Pens.Sienna);

            if (doDrawFunction)
                DrawFunction(Pens.DarkSlateBlue, function2D);
        }

        protected void DrawLine(Pen aPen, float x0, float y0, float x1, float y1)
        {
            float w = (float)this.Width;
            float h = (float)this.Height;
            myGraphic.DrawLine(aPen, x0 + (w / 2.0f), (h / 2.0f) - y0,
                x1 + (w / 2.0f), (h / 2.0f) - y1);
        }

        public void DrawDataPoint(Pen pen, float size, float x, float y)
        {
            int w = this.Width;
            int h = this.Height;

            SolidBrush myBrush = new SolidBrush(pen.Color);

            myGraphic.DrawEllipse(pen, x + w / 2 - size / 2, (float)h / 2 - y - size / 2,
                2 * size, 2 * size);
        }

    }
}

